
import kronos.TimeStampException;
import co.com.certicamara.timeStampObject.exception.TimeStampObjectException;
import co.com.certicamara.timeStampObject.model.TimeStampObjectFactory;
import co.com.certicamara.timeStampObject.model.TimeStampParser;
import co.com.certicamara.timeStampObject.model.TimeStampRequest;

public class TimeStampRequestTest
{
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		if (args.length != 6)
		{
			System.out.println("Uso del componente de generaciOn de estammpas de tiempo:");
			System.out.println("java TimeStampRequestTest [Ruta del certificado digital en formato PKCS#12 utilizado para la solicitud de la estampa] [Password del certificado digital en formato PKCS#12] [URL de la TSA] [Algortimo de Hash] [Ruta completa del archivo a estampar] [Ruta completa donde se almacenarA la estampa de tiempo generada]"); 
		}
		else
		{
			//
			//GeneraciOn de una estampa de tiempo a partir de un archivo
			try
			{
				//
				//Ruta del certificado digital en formato PKCS#12 utilizado para la solicitud de la estampa
				String pkcs12Path = args[0];
				//
				//Password del certificado digital en formato PKCS#12
				String pkcs12password = args[1];
				//
				//URL de la TSA
				String tsaURL = args[2];
				//
				//Algortimo de Hash
				String hashAlgorithm = args[3];
				//
				//Ruta completa del archivo a estampar
				String file2stamp = args[4];
				//
				//Ruta completa donde se almacenarA la estampa de tiempo generada
				String file2saveStamp = args[5];
				
				//
				//Creamos el proveedor de estampado
				TimeStampObjectFactory timeStampObjectFactory = new TimeStampObjectFactory(pkcs12Path, pkcs12password, tsaURL, hashAlgorithm);
				//
				//Creamos el proveedor de comunicaciOn y envio de estampa
				TimeStampRequest timeStampRequest = new TimeStampRequest(timeStampObjectFactory.getCurrentTSPProvider());
				
				//
				//Ejemplo de estampado de un archivo almacenado en el file system
				timeStampRequest.getTimeStamp(file2stamp);
				
				//
				//Almacenamos la estampa de tiempo
				timeStampRequest.getResponse().storeToFile(file2saveStamp);
				
				TimeStampParser timeStampParser = new TimeStampParser(timeStampRequest.getResponse());
				timeStampParser.processStamp();
				System.out.println(setupResponseSucess(timeStampParser));
				
			}
			catch(TimeStampObjectException e)
			{
				System.out.println(setupResponseFail(e.getDescripcion(), e.getCodigo()));
			}
			catch (TimeStampException e)
			{
				System.out.println(setupResponseFail(e.getMessage(), -1));
			}
		}
	}
	
	private static String setupResponseFail(String errorMessage, int errorCode) 
	{
		String response = "";
		response += "<?xml version='1.0' encoding='iso-8859-1'?>\n";
		response += "<CerticamaraResponse>\n";
		response += "<ResponseStatus>\n";
		response += "<Sucess>\n";
		response += "0\n";
		response += "</Sucess>\n";
		response += "<ErrorMessage>\n";
		response += errorMessage + "\n";
		response += "</ErrorMessage>\n";
		response += "<ErrorCode>\n";
		response += errorCode + "\n";
		response += "</ErrorCode>\n";
		response += "</ResponseStatus>\n";
		response += "</CerticamaraResponse>\n";
		
		return response;
	}

	private static String setupResponseSucess(TimeStampParser timeStampParser) 
	{
		String response = "";
		response += "<?xml version='1.0' encoding='iso-8859-1'?>\n";
		response += "<CerticamaraResponse>\n";
		response += "<ResponseStatus>\n";
		response += "<Sucess>\n";
		response += "1\n";
		response += "</Sucess>\n";
		response += "<TimeStamp>\n";
		response += "<Year>\n";
		response += timeStampParser.getYear() + "\n";
		response += "</Year>\n";
		response += "<Month>\n";
		response += timeStampParser.getMonth() + "\n";
		response += "</Month>\n";
		response += "<Day>\n";
		response += timeStampParser.getDay() + "\n";
		response += "</Day>\n";
		response += "<Hour>\n";
		response += timeStampParser.getHour() + "\n";
		response += "</Hour>\n";
		response += "<Minute>\n";
		response += timeStampParser.getMinute() + "\n";
		response += "</Minute>\n";
		response += "<Second>\n";
		response += timeStampParser.getSecond() + "\n";
		response += "</Second>\n";
		response += "<Millisecond>\n";
		response += timeStampParser.getMillisecond() + "\n";
		response += "</Millisecond>\n";
		response += "<Microsecond>\n";
		response += timeStampParser.getMicrosecond() + "\n";
		response += "</Microsecond>\n";
		response += "<TSAName>\n";
		response += timeStampParser.getResponse().getTsaName() + "\n"; 
		response += "</TSAName>\n";
		response += "<TimeStampGeneralInfo>\n";
		response += timeStampParser.getTimeStampInfo() + "\n"; 
		response += "</TimeStampGeneralInfo>\n";
		response += "</TimeStamp>\n";
		response += "</ResponseStatus>\n";
		response += "</CerticamaraResponse>\n";
		
		return response;
	}
}